package com.tanhua.server.service;
import cn.hutool.core.collection.CollUtil;
import com.tanhua.dubbo.api.*;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.enums.ClassifyType;
import com.tanhua.model.enums.ConclusionType;
import com.tanhua.model.enums.CoverType;
import com.tanhua.model.soul.*;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;
@Service
public class TestSoulService {

    @DubboReference
    private OptionApi optionApi;
    @DubboReference
    private ListApi listApi;
    @DubboReference
    private ProblemApi problemApi;
    @DubboReference
    private AnswerApi answerApi;
    @DubboReference
    private UserInfoApi userInfoApi;
    @DubboReference
    private UserPropertyApi userPropertyApi;
    @DubboReference
    private ReportApi reportApi;
    /**
     * 灵魂问卷
     *
     * @return
     */
    public java.util.List<ListVo> findListAndProblem() {

        java.util.List<ListVo> lists = new ArrayList<>();
        //调用api查询问卷
        java.util.List<Lists> listAll = listApi.findListAll();

        //作非空判断
        if (!CollUtil.isEmpty(listAll)) {
            for (Lists listOne : listAll) {
                //调用api查询题目
                java.util.List<Problem> problems = problemApi.findProblemId(listOne.getId());
//        java.util.Lists<Long> ids = CollUtil.getFieldValues(problems, "id", Long.class);
//        Map<Long, Option> map = optionApi.findByIds(ids, null);
                ArrayList<ProblemVo> list = new ArrayList<>();

                if (!CollUtil.isEmpty(problems)) {
                    for (Problem problem : problems) {
                        ProblemVo problemVo = ProblemVo.init(problem);
                        java.util.List<Choices> option = optionApi.findOption(problem.getId());
                        java.util.List<ChoicesVo> choicesVo = ChoicesVo.init(option);
                        BeanUtils.copyProperties(problem, problemVo);
                        problemVo.setOptions(choicesVo);
                        list.add(problemVo);
                    }
                }
                ListVo listVo = ListVo.init(listOne);
                listVo.setQuestions(list);
                //判断当前用户是否存在分析表
                Report byUserId = reportApi.findByUserId(UserHolder.getUserId());
                if(byUserId!=null){
                   listVo.setReportId(byUserId.getId().toString());
                }
                lists.add(listVo);
            }

        }
        return lists;


    }

    /**
     * 提交问卷
     *
     * @param
     */
    public String submitList(Map<String, java.util.List<Answers>> map) {
        java.util.List<Answers> list = map.get("answers");
        //遍历list集合,获取每一个answers对象
        //查找答案表获取到分数
        int sum = 0;
        if (!CollUtil.isEmpty(list)) {
            for (Answers answers : list) {
                int answer = answerApi.findAnswer(Integer.valueOf(answers.getQuestionId())
                        , Integer.valueOf(answers.getOptionId()));
                //计算总分多少
                sum += answer;
            }
        }
        //创建一个userProperty对象
        UserProperty userProperty = new UserProperty();
        Long userId = UserHolder.getUserId();
        userProperty.setUserId(userId);
        //设置用户记录表的总分和人格类型
        if (sum >= 10 && sum <= 20) {
            userProperty.setTotal(sum);
            userProperty.setClassify(ClassifyType.ELEMENTARY.getType());
        } else if (sum >= 21 && sum <= 40) {
            userProperty.setTotal(sum);
            userProperty.setClassify(ClassifyType.INTERMEDIATE.getType());
        } else if (sum >= 41 && sum <= 50) {
            userProperty.setTotal(sum);
            userProperty.setClassify(ClassifyType.ADVANCED.getType());
        }
        //设置用户头像
        UserInfo byId = userInfoApi.findById(userId);
        if (byId != null) {
            userProperty.setAvatar(byId.getAvatar());
        }
        //试卷id和星级
        //获取题目id
        Answers answers = list.get(0);
        //获取对应的题目
        Problem problem = problemApi.findById(Long.valueOf(answers.getQuestionId()));
        //设置试卷id
        userProperty.setListId(problem.getListId());
        //通过试卷id查找星级
        Lists byId1 = listApi.findById(Long.valueOf(problem.getListId()));
        //设置星级和级别
        userProperty.setLevel(byId1.getLevel());
        userProperty.setStar(byId1.getStar());
        //判断之前是否有数据有修改,没有就添加
        UserProperty byUserId = userPropertyApi.findByUserId(userId);

        if(byUserId!=null){
            userPropertyApi.update(userProperty);
            Long  id = byUserId.getId();
            return id.toString();
        }else {
            Long  id = userPropertyApi.save(userProperty);
            return id.toString();
        }


    }

    /**
     * 查看结果
     * @param id
     */
    public ReportVo reportResult(Long id) {
        //通过id获取到用户属性表
        UserProperty userProperty = userPropertyApi.findById(id);
        //创建分析表的实体类
        Report report=new Report();
        //判断用户是什么人格类型
        if(userProperty.getClassify().equals(ClassifyType.ADVANCED.getType())){
            //用户是白兔型设置
            report.setConclusion(ConclusionType.ADVANCED.getType());
            report.setCover(CoverType.ADVANCED.getType());
        }else if(userProperty.getClassify().equals(ClassifyType.INTERMEDIATE.getType())){
            report.setConclusion(ConclusionType.INTERMEDIATE.getType());
            report.setCover(CoverType.INTERMEDIATE.getType());
        }else if(userProperty.getClassify().equals(ClassifyType.ELEMENTARY.getType())){
            report.setConclusion(ConclusionType.ELEMENTARY.getType());
            report.setCover(CoverType.ELEMENTARY.getType());
        }

        //通过星级和总分进行判断
            if(userProperty.getTotal()>10&&userProperty.getTotal()<=30){
                report.setDimensionsKey("外向,判断,抽象,理性");
                report.setDimensionsValue("80%,70%,90%,60%");
            }else if(userProperty.getTotal()<=50&&userProperty.getTotal()>30){
                report.setDimensionsKey("外向,判断,抽象,理性");
                report.setDimensionsValue("60%,90%,90%,80%");
            }
        report.setUserId(userProperty.getUserId());
         //添加report,先查找有report修改,没有就添加
        Report byId = reportApi.findByUserId(UserHolder.getUserId());
        if(byId!=null){
            reportApi.update(report);
        }else {
            reportApi.save(report);
        }

       // 创建一个ReportVo实体类
         ReportVo reportVo=new ReportVo();
         reportVo.init(report);
         reportVo.setConclusion(report.getConclusion());
         reportVo.setCover(report.getCover());
         //与你相似
        java.util.List<Report> dimensions = reportApi.findDimensions(report.getUserId());
        java.util.List<Long> ids = CollUtil.getFieldValues(dimensions, "id", null);
        java.util.List<SimilarYou> similarYous=new ArrayList<>();
                if(!CollUtil.isEmpty(ids)){
                    for (Long uid : ids) {
                        SimilarYou similarYou=new SimilarYou();
                        UserInfo usi = userInfoApi.findById(uid);
                        int id1 = Math.toIntExact(usi.getId());
                        similarYou.setId(id1);
                        similarYou.setAvatar(usi.getAvatar());
                        similarYous.add(similarYou);
                    }
                }
                reportVo.setSimilarYou(similarYous);
                return reportVo;
    }
}
